
namespace Patterns4Net.Core.AttributesPatterns
{
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Mono.Cecil;
    using Patterns4Net.Attributes.DesignPatterns;
    using Patterns4Net.Core.Patterns;
    using Patterns4Net.Attributes;

    [PatternCreatorExport(typeof(ImmutableAttribute))]
    public class ImmutableCreator : ICreatePatternFromAttribute
    {
        public IPattern Crate(CustomAttribute attribute, IMemberDefinition targetType)
        {
            if (attribute.Fields.Count == 1)
            {
                Contract.Assume(attribute.Fields.First().Argument.Value is bool);
                return new Immutable((TypeDefinition)targetType, (bool)attribute.Fields.First().Argument.Value);
            }

            return new Immutable((TypeDefinition)targetType, true);
        }
    }
}
